/*
 * This file is part of the Marmalade SDK Code Samples.
 *
 * (C) 2001-2012 Marmalade. All Rights Reserved.
 *
 * This source code is intended only as a supplement to the Marmalade SDK.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 */

// Marmalade includes
#include "IwUIElementEventHandler.h"
#include "IwUIEvent.h"
#include "IwUITextField.h"

// Project includes
#include "IwUIDailyReport.h"

//-----------------------------------------------------------------------------

class CCreateDailyReportHandler : public CIwUIElementEventHandler
{
public:
    IW_MANAGED_DECLARE(CCreateDailyReportHandler)
    CCreateDailyReportHandler() {}

private:
    // CIwUIElementEventHandler virtuals
    virtual void Activate(bool val)
    {
        CIwUIElementEventHandler::Activate(val);

        if (val)
        {
            IW_UI_CREATE_HANDLER_SLOT1(this, CCreateDailyReportHandler,
                OkCreateDailyReport, CIwUIElement*);
            IW_UI_CREATE_HANDLER_SLOT1(this, CCreateDailyReportHandler,
                CancelCreateDailyReport, CIwUIElement*);

            CIwUIElement* pElement = GetElement();
			pElement->LookupChildNamed(m_TaskName, "TaskVal");
            pElement->LookupChildNamed(m_BeginDate, "BeginDateVal");
            pElement->LookupChildNamed(m_StartTime, "StartTimeVal");
            pElement->LookupChildNamed(m_EndTime, "EndTimeVal");
			pElement->LookupChildNamed(m_Describe, "DesVal");
        }
        else
        {
            IW_UI_DESTROY_HANDLER_SLOTS(this);
        }
    }

    virtual void Clone(CIwUIElementEventHandler* pTarget) const
    {
        IW_UI_CLONE_SUPERCLASS(pTarget, CCreateDailyReportHandler, CIwUIElementEventHandler)
    }

    virtual bool HandleEvent(CIwEvent* pEvent)
    {
        if (pEvent->GetID() == IWUI_EVENT_VISIBILITY)
        {
            CIwUIEventVisibility* pEventVisibility =
                IwSafeCast<CIwUIEventVisibility*>(pEvent);

            // Is our element becoming visible?
            if ((pEventVisibility->GetElement() == GetElement()) &&
                GetElement()->IsVisible())
            {
                DailyReportField taskName;
				DailyReportField beginDate;
                DailyReportField startTime;
                DailyReportField endTime;
				DailyReportField describe;

                GetApp()->GetInitialDailyReport(taskName, beginDate, startTime, endTime, describe);
				//const char* taskN = taskName;
                m_TaskName->SetCaption(taskName);
                m_BeginDate->SetCaption(beginDate);
                m_StartTime->SetCaption(startTime);
				m_EndTime->SetCaption(endTime);
				m_Describe->SetCaption(describe);
            }
        }

        return CIwUIElementEventHandler::HandleEvent(pEvent);
    }

    // Slot functions
    void OkCreateDailyReport(CIwUIElement*)
    {
        const char* pTaskName = m_TaskName->GetCaption();
        const char* pBeginDate = m_BeginDate->GetCaption();
        const char* pStartTime = m_StartTime->GetCaption();
		const char* pEndTime = m_EndTime->GetCaption();
		const char* pDescribe = m_Describe->GetCaption();

        GetApp()->CreateConfirmed(pTaskName, pBeginDate, pStartTime, pEndTime, pDescribe);
        GetApp()->HideCreateDialog();
    }

    void CancelCreateDailyReport(CIwUIElement*)
    {
        GetApp()->HideCreateDialog();
    }

    // Member data
	CIwUITextField* m_TaskName;
    CIwUITextField* m_BeginDate;
    CIwUITextField* m_StartTime;
    CIwUITextField* m_EndTime;
	CIwUITextField* m_Describe;
};

IW_MANAGED_IMPLEMENT_FACTORY(CCreateDailyReportHandler)
